<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sorting Arrays</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="array.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.array.html">数组 函数</a></div>
 <div class="up"><a href="book.array.html">数组</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="array.sorting" class="chapter">
 <h1>Sorting Arrays</h1>

 <p class="para">
  PHP has several functions that deal with sorting arrays, and this
  document exists to help sort it all out.
 </p>
 <p class="para">
  The main differences are:
 </p>
 <p class="para">
  <ul class="simplelist">
   <li class="member">
    Some sort based on the <span class="type"><a href="language.types.array.html" class="type array">array</a></span> keys, whereas others by
    the values: <em>$array[&#039;key&#039;] = &#039;value&#039;;</em>
   </li>
   <li class="member">
    Whether or not the correlation between the keys and values are
    maintained after the sort, which may mean the keys are reset
    numerically (0,1,2 ...)
   </li>
   <li class="member">
    The order of the sort: alphabetical, low to high (ascending),
    high to low (descending), numerical, natural, random, or user defined
   </li>
   <li class="member">
    Note: All of these sort functions act directly on the array
    variable itself, as opposed to returning a new sorted array
   </li>
   <li class="member">
    If any of these sort functions evaluates two members as equal then the
    order is undefined (the sorting is not stable).
   </li>
  </ul>
 </p>
 <p class="para">
  <table class="doctable table">
   <caption><strong>Sorting function attributes</strong></caption>
   
    <thead>
     <tr>
      <th>Function name</th>
      <th>Sorts by</th>
      <th>Maintains key association</th>
      <th>Order of sort</th>
      <th>Related functions</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td><span class="function"><a href="function.array-multisort.html" class="function">array_multisort()</a></span></td>
      <td>value</td>
      <td>associative yes, numeric no</td>
      <td>first array or sort options</td>
      <td><span class="function"><a href="function.array-walk.html" class="function">array_walk()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.asort.html" class="function">asort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>low to high</td>
      <td><span class="function"><a href="function.arsort.html" class="function">arsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.arsort.html" class="function">arsort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>high to low</td>
      <td><span class="function"><a href="function.asort.html" class="function">asort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.krsort.html" class="function">krsort()</a></span></td>
      <td>key</td>
      <td>yes</td>
      <td>high to low</td>
      <td><span class="function"><a href="function.ksort.html" class="function">ksort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.ksort.html" class="function">ksort()</a></span></td>
      <td>key</td>
      <td>yes</td>
      <td>low to high</td>
      <td><span class="function"><a href="function.asort.html" class="function">asort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.natcasesort.html" class="function">natcasesort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>natural, case insensitive</td>
      <td><span class="function"><a href="function.natsort.html" class="function">natsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.natsort.html" class="function">natsort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>natural</td>
      <td><span class="function"><a href="function.natcasesort.html" class="function">natcasesort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.rsort.html" class="function">rsort()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>high to low</td>
      <td><span class="function"><a href="function.sort.html" class="function">sort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.shuffle.html" class="function">shuffle()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>random</td>
      <td><span class="function"><a href="function.array-rand.html" class="function">array_rand()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.sort.html" class="function">sort()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>low to high</td>
      <td><span class="function"><a href="function.rsort.html" class="function">rsort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.uasort.html" class="function">uasort()</a></span></td>
      <td>value</td>
      <td>yes</td>
      <td>user defined</td>
      <td><span class="function"><a href="function.uksort.html" class="function">uksort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.uksort.html" class="function">uksort()</a></span></td>
      <td>key</td>
      <td>yes</td>
      <td>user defined</td>
      <td><span class="function"><a href="function.uasort.html" class="function">uasort()</a></span></td>
     </tr>

     <tr>
      <td><span class="function"><a href="function.usort.html" class="function">usort()</a></span></td>
      <td>value</td>
      <td>no</td>
      <td>user defined</td>
      <td><span class="function"><a href="function.uasort.html" class="function">uasort()</a></span></td>
     </tr>

    </tbody>
   
  </table>

 </p>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="array.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.array.html">数组 函数</a></div>
 <div class="up"><a href="book.array.html">数组</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
